package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcBasicService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 账户
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/basic")
@RestController
public class XqcBasicController {

    /** 接口调用服务 */
    @Autowired
    private IXqcBasicService basicService;


    /**
     * 删除物料
     */
    @PostMapping("/delete_material")
    @VtuzxApi
    public Object deleteMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        List<String> ids = VtuzxUtil.getObject(param, "ids");
        basicService.deleteMaterial(token, ids);
        return null;
    }

    /**
     * 查询物料
     */
    @PostMapping("/get_material_by_key")
    @VtuzxApi
    public Object getMaterialByKey(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String key = VtuzxUtil.getString(param, "key");
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize);
        return basicService.getMaterial(token, key, pageSize);
    }

    /**
     * 查询物料
     */
    @PostMapping("/get_material")
    @VtuzxApi
    public Object getMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return basicService.getMaterial(token, param);
    }

    /**
     * 刷新物料
     */
    @PostMapping("/sync_material")
    @VtuzxApi
    public Object syncMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        basicService.syncMaterial(token, param);
        return null;
    }

    /** 获取同步物料处理结果 */
    @PostMapping("/get_material_reload_result")
    @VtuzxApi
    public Object getMaterialReloadResult(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return basicService.getMaterialReloadResult(token, param);
    }

    /**
     * 删除仓库
     */
    @PostMapping("/delete_stock")
    @VtuzxApi
    public Object deleteStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        List<String> ids = VtuzxUtil.getObject(param, "ids");
        basicService.deleteStock(token, ids);
        return null;
    }

    /**
     * 查询仓库
     */
    @PostMapping("/get_stock")
    @VtuzxApi
    public Object getStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return basicService.getStock(token, param);
    }

    /**
     * 刷新仓库
     */
    @PostMapping("/sync_stock")
    @VtuzxApi
    public Object syncStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        basicService.syncStock(token, param);
        return null;
    }

    /** 获取同步仓库处理结果 */
    @PostMapping("get_stock_reload_result")
    @VtuzxApi
    public Object getStockReloadResult(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return basicService.getStockReloadResult(token, param);
    }

    /**
     * 删除供应商
     */
    @PostMapping("/delete_supplier")
    @VtuzxApi
    public Object deleteSupplier(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String fNumber = VtuzxUtil.getObject(param, "fNumber");
        basicService.deleteSupplier(token, fNumber);
        return null;
    }

    /**
     *恢复供应商(逻辑删除)
     */
    @PostMapping("/restore_supplier")
    @VtuzxApi
    public Object restoreSupplier(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String fNumber = VtuzxUtil.getString(param, "fNumber");
        basicService.restoreSupplier(token, fNumber);
        return null;
    }

    /**
     * 查询供应商
     */
    @PostMapping("/get_supplier_by_key")
    @VtuzxApi
    public Object getSupplierByKey(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String key = VtuzxUtil.getString(param, "key");
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        return basicService.getSupplierByKey(token, key, pageSize, pageIndex);
    }

    /**
     * 查询供应商
     */
    @PostMapping("/get_supplier")
    @VtuzxApi
    public Object getSupplier(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return basicService.getSupplier(token, param);
    }

    /**
     * 刷新供应商
     */
    @PostMapping("/sync_supplier")
    @VtuzxApi
    public Object syncSupplier(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> p) {
        basicService.syncSupplier(token, p);
        return null;
    }

    /** 获取同步供应商处理结果 */
    @PostMapping("get_supplier_reload_result")
    @VtuzxApi
    public Object getSupplierReloadResult(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> p) {
        return basicService.getSupplierReloadResult(token, p);
    }

    /** 获取仓库的仓位列表 */
    @PostMapping("get_stock_locations")
    @VtuzxApi
    public Object getStockLocations(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        return basicService.getStockLocations(stockNo);
    }

    /** 获取仓位信息 */
    @PostMapping("get_stock_location")
    @VtuzxApi
    public Object getStockLocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        return basicService.getStockLocation(stockNo, locationNo);
    }

    /**
     * 获取仓库详细
     */
    @PostMapping("/get_stock_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "fNumber", label = "编码", notNull = true)
    } )
    public Object getStockDetail(@RequestBody Map<String, Object> param) throws VtuzxException {
        String fNumber = VtuzxUtil.getString(param, "fNumber");
        return basicService.getStockDetail(fNumber);
    }

    /**
     * 保存仓库详细（仓位）
     */
    @PostMapping("/save_stock_detail")
    @VtuzxApi
    public Object saveStockDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        return basicService.saveStockDetail(param);
    }

    /**
     * 获取用户的仓库
     */
    @PostMapping("/get_customer_stock")
    @VtuzxApi
    public Object getCustomerStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        return basicService.getCustomerStock(_userId);
    }

    /**
     * 获取用户的仓库
     */
    @PostMapping("/stock_to_get_location")
    @VtuzxApi
    public Object stockToGetLocation(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        return basicService.stockToGetLocation(stockNo);
    }



    /**
     * 获取部门
     */
    @PostMapping("/get_department")
    @VtuzxApi
    public Object getDepartment(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        basicService.getDepartment(token, param);
        return null;
    }

    /**
     * 查询部门列表
     */
    @PostMapping("/get_department_list")
    @VtuzxApi
    public Object getDepartmentList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return basicService.getDepartmentList(token, param);
    }


    /**
     * 查询员工
     */
    @PostMapping("/get_emp_info")
    @VtuzxApi
    public Object getEmpInfo(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        basicService.getEmpInfo(token, param);
        return null;
    }


    /**
     * 新增托盘信息
     */
    @PostMapping("/add_tray")
    @VtuzxApi
    public Object addTray(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        basicService.addTray(token, param);
        return null;
    }

    /**
     * 查询托盘信息
     */
    @PostMapping("/get_tray_list")
    @VtuzxApi
    public Object getTrayList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return basicService.getTrayList(token, param);
    }

    /**
     * 更新托盘信息
     */
    @PostMapping("/update_tray")
    @VtuzxApi
    public Object updateTray(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        basicService.updateTray(token, param);
        return null;
    }

    /**
     * 删除托盘信息
     */
    @PostMapping("/delete_tray")
    @VtuzxApi
    public Object deleteTray(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        basicService.deleteTray(token, param);
        return null;
    }



    /**
     * 删除BOM
     */
    @PostMapping("/delete_bom")
    @VtuzxApi
    public Object deleteBom(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        List<String> ids = VtuzxUtil.getObject(param, "ids");
        basicService.deleteBom(token, ids);
        return null;
    }

    /**
     * 查询BOM（成品，半成品）
     */
    @PostMapping("/get_bom_by_key")
    @VtuzxApi
    public Object getBomByKey(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String key = VtuzxUtil.getString(param, "key");
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize);
        return basicService.getBom(token, key, pageSize, pageIndex);
    }

    /**
     * 查询BOM
     */
    @PostMapping("/get_bom")
    @VtuzxApi
    public Object getBom(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return basicService.getBom(token, param);
    }

    /**
     * 刷新BOM
     */
    @PostMapping("/sync_bom")
    @VtuzxApi
    public Object syncBom(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        basicService.syncBom(token, param);
        return null;
    }

    /** 获取同步BOM处理结果 */
    @PostMapping("get_bom_reload_result")
    @VtuzxApi
    public Object getBomReloadResult(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return basicService.getBomReloadResult(token, param);
    }


    /** 更新供应商 */
    @PostMapping("update_supplier")
    @VtuzxApi
    public Object updateSupplier(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        basicService.updateSupplier(token, param);
        return null;
    }

    /** 创建追溯码 */
    @PostMapping("create_traceability_code")
    @VtuzxApi
    public Object createTraceabilityCode(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws InterruptedException, VtuzxException {
        basicService.createTraceabilityCode(token, param);
        return null;
    }

    /** 获取追溯码列表 */
    @PostMapping("get_traceability_code_list")
    @VtuzxApi
    public Object getTraceabilityCodeList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return basicService.getTraceabilityCodeList(token, param);
    }

    /** 获取追溯码详细 */
    @PostMapping("get_traceability_code_detail")
    @VtuzxApi
    public Object getTraceabilityCodeDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return basicService.getTraceabilityCodeDetail(token, param);
    }

    /** 删除追溯码 */
    @PostMapping("delete_traceability_code")
    @VtuzxApi
    public Object deleteTraceabilityCode(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        basicService.deleteTraceabilityCode(token, param);
        return null;
    }

    /** 更新物料 */
    @PostMapping("update_material")
    @VtuzxApi
    public Object updateMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        basicService.updateMaterial(token, param);
        return null;
    }

    /**
     * 完成任务查询物料
     */
    @PostMapping("/save_get_material_by_key")
    @VtuzxApi
    public Object saveGetMaterialByKey(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        String key = VtuzxUtil.getString(param, "key");
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        return basicService.saveGetMaterialByKey(token, key, pageSize, pageIndex);
    }

    /**
     * 导出物料列表
     */
    @PostMapping("/download_material")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return basicService.downloadMaterial(token, param);
    }

    /**
     * 导出托盘
     */
    @PostMapping("/download_basic_tray_list")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadTrayList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return basicService.downloadTrayList(token, param);
    }
}
